#######################################################################################################################
# File Name  : Makefile.derivative.memorymap                                                                          #
# Description: Memory size definition for each supported derivative                                                   #
# Project    : Vector Basic Runtime System                                                                            #
# Module     : BrsHw for Platform RH850                                                                               #
#                                                                                                                     #
#---------------------------------------------------------------------------------------------------------------------#
# COPYRIGHT                                                                                                           #
#---------------------------------------------------------------------------------------------------------------------#
# Copyright (c) 2018 by Vector Informatik GmbH.                                                  All rights reserved. #
#                                                                                                                     #
#---------------------------------------------------------------------------------------------------------------------#
# AUTHOR IDENTITY                                                                                                     #
#---------------------------------------------------------------------------------------------------------------------#
# Name                          Initials      Company                                                                 #
# ----------------------------  ------------  ------------------------------------------------------------------------#
# Benjamin Walter               visbwa        Vector Informatik GmbH                                                  #
# Johannes Kempf                viskj         Vector Informatik GmbH                                                  #
# Nika Hamidi                   vinhi         Vector Informatik GmbH                                                  #
# Francois Berjonneau           vfpfsb        Vector France SAS                                                       #
# Sascha Mauser                 vismaa        Vector Informatik GmbH                                                  #
# Muhammad Suliman              vismun        Vector Informatik GmbH                                                  #
# Thomas Wiesgickl              virtwl        Vector Informatik GmbH                                                  #
#---------------------------------------------------------------------------------------------------------------------#
# REVISION HISTORY                                                                                                    #
#---------------------------------------------------------------------------------------------------------------------#
# Version   Date        Author  Description                                                                           #
# --------  ----------  ------  --------------------------------------------------------------------------------------#
# 01.00.00  2014-07-08  visbwa  Initial version                                                                       #
# 01.01.00  2016-06-29  viskj   Adaptions for support of RAM initialization ('k' after ram-size)                      #
# 01.02.00  2016-06-30  visbwa  Changed header information according to Brs_Template 2.00.02                          #
# 01.03.00  2016-07-11  visnhi  Added required section for F1H family                                                 #
# 01.03.01  2017-06-06  visbwa  Added checks of Codeflash size for F1H                                                #
# 01.03.02  2017-10-12  vfpfsb  Added required section for C1H family                                                 #
# 01.03.03  2017-10-24  vfpfsb  Fixed C1H support for RAM section definition and for GHS/DIAB/IAR compiler            #
# 01.03.04  2017-12-20  vismaa  changed mapping of C1M_AZ and P1H family for proper MultiCore support,                #
#                               added configurable start address for codeflash bank B,                                #
#                               added global ram bank A and B (needed for different ram sizes)                        #
#           2018-01-18  visbwa  Review/fix                                                                            #
# 01.03.05  2018-02-19  vismun  Added F1KH                                                                            #
# 01.03.06  2018-05-14  virtwl  Added support of GlobalRAM for F1KM_S4                                                #
#######################################################################################################################

###########################################################################################################
####  Check for all necessary/used variables                                                           ####
###########################################################################################################
ifeq ($(DERIVATIVE_GROUP_$(DERIVATIVE)),)
 $(error "Derivative Group for your DERIVATIVE is not yet defined within Makefile.derivative.settings.")
endif

ifeq ($(DERIVATIVE_GROUP_$(DERIVATIVE)),$(filter $(DERIVATIVE_GROUP_$(DERIVATIVE)),F1H C1H C1M_AZ P1H F1KH))
  ifeq ($(GLOBAL_RAM_SIZE_BANKA_$(DERIVATIVE)),)
    $(error "Global RAM size of Bank A for your DERIVATIVE is not yet defined within Makefile.derivative.settings.")
  endif

  ifeq ($(GLOBAL_RAM_SIZE_BANKB_$(DERIVATIVE)),)
    $(error "Global RAM size of Bank B for your DERIVATIVE is not yet defined within Makefile.derivative.settings.")
  endif

  ifeq ($($(DERIVATIVE_GROUP_$(DERIVATIVE))_CODEFLASH_START_ADDRESS_BANKB_$(CODEFLASH_SIZE_BANKB_$(DERIVATIVE))),)
  # The most families start CodeFlash Bank B at 0x00800000.
  # If one derivative differs, you can specify a derivative/family specific addresse within Makefile.derivative.settings
  $(DERIVATIVE_GROUP_$(DERIVATIVE))_CODEFLASH_START_ADDRESS_BANKB_$(CODEFLASH_SIZE_BANKB_$(DERIVATIVE)) = 0x00800000
  endif

  ifeq ($(CODEFLASH_SIZE_BANKA_$(DERIVATIVE)),)
   $(error "CodeFlash size of Bank A for your DERIVATIVE is not yet defined within Makefile.derivative.settings.")
  endif

  ifeq ($(CODEFLASH_SIZE_BANKB_$(DERIVATIVE)),)
   $(error "CodeFlash size of Bank B for your DERIVATIVE is not yet defined within Makefile.derivative.settings.")
  endif

  ifeq ($(LOCAL_RAM_SIZE_CORE0_$(DERIVATIVE)),)
   $(error "RAM size for your DERIVATIVE is not yet defined within Makefile.derivative.settings.")
  endif
  
  ifeq ($(LOCAL_RAM_SIZE_CORE1_$(DERIVATIVE)),)
   $(error "RAM size for your DERIVATIVE is not yet defined within Makefile.derivative.settings.")
  endif

  ifeq ($($(DERIVATIVE_GROUP_$(DERIVATIVE))_LOCAL_RAM_START_ADDRESS_CORE0_$(LOCAL_RAM_SIZE_CORE0_$(DERIVATIVE))),)
   $(error "RAM start address $(DERIVATIVE_GROUP_$(DERIVATIVE))_LOCAL_RAM_START_ADDRESS_CORE0_$(LOCAL_RAM_SIZE_CORE0_$(DERIVATIVE)) is not yet defined within Makefile.derivative.settings.")
  endif

  ifeq ($($(DERIVATIVE_GROUP_$(DERIVATIVE))_LOCAL_RAM_START_ADDRESS_CORE1_$(LOCAL_RAM_SIZE_CORE1_$(DERIVATIVE))),)
   $(error "RAM start address $(DERIVATIVE_GROUP_$(DERIVATIVE))_LOCAL_RAM_START_ADDRESS_CORE1_$(LOCAL_RAM_SIZE_CORE1_$(DERIVATIVE)) is not yet defined within Makefile.derivative.settings.")
  endif

else

  ifeq ($(DERIVATIVE_GROUP_$(DERIVATIVE)),$(filter $(DERIVATIVE_GROUP_$(DERIVATIVE)),F1KM_S4))
    ifeq ($(GLOBAL_RAM_SIZE_BANKA_$(DERIVATIVE)),)
      $(error "Global RAM size of Bank A for your DERIVATIVE is not yet defined within Makefile.derivative.settings.")
    endif

    ifeq ($(GLOBAL_RAM_SIZE_BANKB_$(DERIVATIVE)),)
      $(error "Global RAM size of Bank B for your DERIVATIVE is not yet defined within Makefile.derivative.settings.")
    endif

    ifeq ($(CODEFLASH_SIZE_BANKA_$(DERIVATIVE)),)
     $(error "CodeFlash size of Bank A for your DERIVATIVE is not yet defined within Makefile.derivative.settings.")
    endif

    ifeq ($(LOCAL_RAM_SIZE_CORE0_$(DERIVATIVE)),)
     $(error "RAM size for your DERIVATIVE is not yet defined within Makefile.derivative.settings.")
    endif

    ifeq ($($(DERIVATIVE_GROUP_$(DERIVATIVE))_LOCAL_RAM_START_ADDRESS_CORE0_$(LOCAL_RAM_SIZE_CORE0_$(DERIVATIVE))),)
     $(error "RAM start address $(DERIVATIVE_GROUP_$(DERIVATIVE))_LOCAL_RAM_START_ADDRESS_CORE0_$(LOCAL_RAM_SIZE_CORE0_$(DERIVATIVE)) is not yet defined within Makefile.derivative.settings.")
    endif

  else
    ifeq ($(GLOBAL_RAM_SIZE_$(DERIVATIVE)),)
      $(error "Global RAM size for your DERIVATIVE is not yet defined within Makefile.derivative.settings.")
    endif

    ifeq ($($(DERIVATIVE_GROUP_$(DERIVATIVE))_RAM_START_ADDRESS_$(GLOBAL_RAM_SIZE_$(DERIVATIVE))),)
     $(error "Global RAM start address $(DERIVATIVE_GROUP_$(DERIVATIVE))_RAM_START_ADDRESS_$(GLOBAL_RAM_SIZE_$(DERIVATIVE)) is not yet defined within Makefile.derivative.settings.")
    endif
  endif

endif

###########################################################################################################
####  MEMORY MAP FOR GHS / DIAB                                                                        ####
###########################################################################################################
ifeq ($(DERIVATIVE_GROUP_$(DERIVATIVE)),$(filter $(DERIVATIVE_GROUP_$(DERIVATIVE)),F1H C1H C1M_AZ P1H F1KH))
  define MEMORY_ALLGROUPS
    $(ECHO) "  iROM_0A    : ORIGIN = 0x00000000,"                                                                                                "LENGTH = $(CODEFLASH_SIZE_BANKA_$(DERIVATIVE))k"  >> $@; \
    $(ECHO) "  iROM_0B    : ORIGIN = $($(DERIVATIVE_GROUP_$(DERIVATIVE))_CODEFLASH_START_ADDRESS_BANKB_$(CODEFLASH_SIZE_BANKB_$(DERIVATIVE))),"  "LENGTH = $(CODEFLASH_SIZE_BANKB_$(DERIVATIVE))k"  >> $@; \
    $(ECHO) "  iROM_1     : ORIGIN = 0x01000000,"                                                                                                "LENGTH = 32k"                                     >> $@; \
    $(ECHO) "  iRAM_CORE0 : ORIGIN = $($(DERIVATIVE_GROUP_$(DERIVATIVE))_LOCAL_RAM_START_ADDRESS_CORE0_$(LOCAL_RAM_SIZE_CORE0_$(DERIVATIVE))),"  "LENGTH = $(LOCAL_RAM_SIZE_CORE0_$(DERIVATIVE))k"  >> $@; \
    $(ECHO) "  iRAM_CORE1 : ORIGIN = $($(DERIVATIVE_GROUP_$(DERIVATIVE))_LOCAL_RAM_START_ADDRESS_CORE1_$(LOCAL_RAM_SIZE_CORE1_$(DERIVATIVE))),"  "LENGTH = $(LOCAL_RAM_SIZE_CORE1_$(DERIVATIVE))k"  >> $@; \
    $(ECHO) "  gRAM_A     : ORIGIN = $($(DERIVATIVE_GROUP_$(DERIVATIVE))_GLOBAL_RAM_BANK_A_START_ADDRESS_$(GLOBAL_RAM_SIZE_BANKA_$(DERIVATIVE))), LENGTH = $(GLOBAL_RAM_SIZE_BANKA_$(DERIVATIVE))k" >> $@; \
    $(ECHO) "  gRAM_B     : ORIGIN = $($(DERIVATIVE_GROUP_$(DERIVATIVE))_GLOBAL_RAM_BANK_B_START_ADDRESS_$(GLOBAL_RAM_SIZE_BANKB_$(DERIVATIVE))), LENGTH = $(GLOBAL_RAM_SIZE_BANKB_$(DERIVATIVE))k" >> $@; 
  endef

else
  ifeq ($(DERIVATIVE_GROUP_$(DERIVATIVE)),$(filter $(DERIVATIVE_GROUP_$(DERIVATIVE)),F1KM_S4))
    define MEMORY_ALLGROUPS
      $(ECHO) "  iROM_0A    : ORIGIN = 0x00000000,"                                                                                                "LENGTH = $(CODEFLASH_SIZE_BANKA_$(DERIVATIVE))k"  >> $@; \
      $(ECHO) "  iROM_1     : ORIGIN = 0x01000000,"                                                                                                "LENGTH = 32k"                                     >> $@; \
      $(ECHO) "  iRAM_CORE0 : ORIGIN = $($(DERIVATIVE_GROUP_$(DERIVATIVE))_LOCAL_RAM_START_ADDRESS_CORE0_$(LOCAL_RAM_SIZE_CORE0_$(DERIVATIVE))),"  "LENGTH = $(LOCAL_RAM_SIZE_CORE0_$(DERIVATIVE))k"  >> $@; \
      $(ECHO) "  gRAM_A     : ORIGIN = $($(DERIVATIVE_GROUP_$(DERIVATIVE))_GLOBAL_RAM_BANK_A_START_ADDRESS_$(GLOBAL_RAM_SIZE_BANKA_$(DERIVATIVE))), LENGTH = $(GLOBAL_RAM_SIZE_BANKA_$(DERIVATIVE))k" >> $@; \
      $(ECHO) "  gRAM_B     : ORIGIN = $($(DERIVATIVE_GROUP_$(DERIVATIVE))_GLOBAL_RAM_BANK_B_START_ADDRESS_$(GLOBAL_RAM_SIZE_BANKB_$(DERIVATIVE))), LENGTH = $(GLOBAL_RAM_SIZE_BANKB_$(DERIVATIVE))k" >> $@; 
    endef

  else
    define MEMORY_ALLGROUPS
  $(ECHO) "  /* FLASH (User Area) */                              " >> $@; \
  $(ECHO) "  iVect:         ORIGIN = 0x00000000, LENGTH = 0x000100" >> $@; \
  $(ECHO) "  iBootM:         ORIGIN = 0x00001600, LENGTH = 0x000030" >> $@; \
  $(ECHO) "  iFbl:          ORIGIN = 0x00001800, LENGTH = 0x01C000" >> $@; \
  $(ECHO) "  iOTP:          ORIGIN = 0x0001f800, LENGTH = 0x000800" >> $@; \
  $(ECHO) "  iApplHeader:   ORIGIN = 0x00020000, LENGTH = 0x000100" >> $@; \
  $(ECHO) "  iAppl:         ORIGIN = 0x00020100, LENGTH = 0x01F000" >> $@; \
  $(ECHO) "  iEeprom:       ORIGIN = 0xFF200000, LENGTH = 0x000200" >> $@; \
  $(ECHO) "                                                       " >> $@; \
  $(ECHO) "  /* RAM */                                            " >> $@; \
  $(ECHO) "  iFlashDrv:     ORIGIN = 0xFEBE0000, LENGTH = 0x003000" >> $@; \
  $(ECHO) "  iRAM:          ORIGIN = 0xFEBE3200, LENGTH = 0x00CE00" >> $@; \
  $(ECHO) "  iStack:        ORIGIN = 0xFEBF1000, LENGTH = 0x001000" >> $@;
    endef
  endif
endif

###########################################################################################################
####  MEMORY MAP FOR IAR COMPILER                                                                      ####
###########################################################################################################
ifeq ($(DERIVATIVE_GROUP_$(DERIVATIVE)),$(filter $(DERIVATIVE_GROUP_$(DERIVATIVE)),F1H C1H C1M_AZ P1H F1KH))
  define MEMORY_ALLGROUPS_IAR
    $(ECHO) "define region ROM_near       = mem:[from 0x00000000 size 0x8000];"                                 >> $@; \
    $(ECHO) "define region ROM_A_region   = mem:[from 0x00000000 size $(CODEFLASH_SIZE_BANKA_$(DERIVATIVE))k];" >> $@; \
    $(ECHO) "define region ROM_B_region   = mem:[from $($(DERIVATIVE_GROUP_$(DERIVATIVE))_CODEFLASH_START_ADDRESS_BANKB_$(CODEFLASH_SIZE_BANKB_$(DERIVATIVE))) size $(CODEFLASH_SIZE_BANKB_$(DERIVATIVE))k];"        >> $@; \
    $(ECHO) "define region RAM_PRI_region_Core0 = mem:[from $($(DERIVATIVE_GROUP_$(DERIVATIVE))_LOCAL_RAM_START_ADDRESS_CORE0_$(LOCAL_RAM_SIZE_CORE0_$(DERIVATIVE))) size $(LOCAL_RAM_SIZE_CORE0_$(DERIVATIVE))k]; " >> $@; \
    $(ECHO) "define region RAM_PRI_region_Core1 = mem:[from $($(DERIVATIVE_GROUP_$(DERIVATIVE))_LOCAL_RAM_START_ADDRESS_CORE1_$(LOCAL_RAM_SIZE_CORE1_$(DERIVATIVE))) size $(LOCAL_RAM_SIZE_CORE1_$(DERIVATIVE))k]; " >> $@;
  endef

else
  define MEMORY_ALLGROUPS_IAR
    $(ECHO) "define region ROM_near             = mem:[from 0x00000000 size 0x8000];"                           >> $@; \
    $(ECHO) "define region ROM_A_region         = mem:[from 0x00000000 size $(CODEFLASH_SIZE_$(DERIVATIVE))k];" >> $@; \
    $(ECHO) "define region RAM_PRI_region_Core0 = mem:[from $($(DERIVATIVE_GROUP_$(DERIVATIVE))_RAM_START_ADDRESS_$(GLOBAL_RAM_SIZE_$(DERIVATIVE))) size $(GLOBAL_RAM_SIZE_$(DERIVATIVE))k]; " >> $@;
  endef
endif